package com.D5Power.Objects
{
	import com.D5Power.Controler.Actions;
	import com.D5Power.Controler.BaseControler;
	import com.D5Power.Controler.BaseLuaControler;
	import com.D5Power.Controler.NCharacterControler;
	import com.D5Power.GMath.data.qTree.QTree;
	import com.D5Power.Lua.LuaDispatch;
	import com.D5Power.Objects.Effects.FeetStep;
	import com.D5Power.Stuff.CharacterStuff;
	import com.D5Power.Stuff.HSpbar;
	import com.D5Power.basic.GraphicsResource;
	import com.D5Power.display.D5TextField;
	import com.D5Power.ns.ControllerAndTarget;
	import com.D5Power.scene.BaseScene;
	import com.D5Power.scene.D5Scene;
	import com.D5Power.utils.XYArray;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import luaAlchemy.LuaAlchemy;

	use namespace ControllerAndTarget;
	/**
	 * 可控制的（具备控制器的）动画对象
	 */ 
	public class ControlActionObject extends ActionObject implements LuaDispatch 
	{
		/**
		 * 在渲染的过程中，Y轴的调整。使用见渲染器。
		 */ 
		public var renderY:int=0;
		/**
		 * 控制器
		 */ 
		protected var _controler:BaseControler;
		
		/**
		 * Lua 脚本执行器
		 */ 
		protected var myLua:LuaAlchemy;
		
		/**
		 * Lua 脚本控制器
		 */ 
		protected var _lua_controler:BaseLuaControler;
		
		/**
		 * HP进度条及其他静态属性
		 */ 
		protected var _StuffList:Array;
		
		/**
		 * HP值
		 */ 
		protected var _hp:uint;
		/**
		 * HP最大值
		 */ 
		public var hpMax:uint;
		/**
		 * SP值
		 */ 
		protected var _sp:uint;
		/**
		 * SP最大值
		 */ 
		public var spMax:uint;
		
		/**
		 * 目标
		 */ 
		public var target:ActionObject
		
		/**
		 * 跟随方向
		 */ 
		public var targetDirection:uint;
		
		/**
		 * Lua脚本文件名
		 */ 
		public var lua_filename:String='';
		
		/**
		 * 脚印文件
		 */ 
		public var feetFile:String='';
		
		/**
		 * 脚印时间
		 */ 
		protected var _feetTime:uint=0;
		
		/**
		 * 脚印花费时间
		 */ 
		protected var _feetSpend:uint=0;
		
		/**
		 * 脚印左右脚标记
		 */ 
		protected var _feetLOR:Boolean=false;
		
		public function get controler():BaseControler
		{
			return _controler;
		}
		
		override public function calcAction():void
		{
			_controler.calcAction();
		}
		
		public function ControlActionObject(ctrl:BaseControler)
		{
			super();
			_controler = ctrl;
			_controler.me=this;
			objectName = 'ControlActionObject';
		}
		
		/**
		 * 增加HP/SP显示条
		 */ 
		public function addStuff(stuff:CharacterStuff):void
		{
			if(_StuffList==null) _StuffList=new Array();
			if(_StuffList.indexOf(stuff)!=-1) return;
			_StuffList.push(stuff);
		}
		
		/**
		 * 
		 */ 
		public function removeStuff(id:int = -1):void
		{
			if(id==-1)
			{
				while(_StuffList.length)
				{
					_StuffList[0].clear();
					_StuffList.splice(0,1);
				}
			}else{
				if(_StuffList[id]==null) return;
				_StuffList[id].clear();
				_StuffList.splice(id,1);
			}
			
		}
		
		/**
		 * 获取显示条列表
		 */ 
		public function get StuffList():Array
		{
			return _StuffList;
		}
		
		public function set hp(val:int):void
		{
			_hp = val>0 ? val : 0;
		}
		
		public function get hp():int
		{
			return _hp;
		}
		
		override public function set action(flag:int):void
		{
			super.action = flag;
			if(action==Actions.Die) Loop=false;
			
			_FrameTotal = _graphics.getFrameTotal(Math.abs(directionNum));
		}
		
		/**
		 * 有控制器的一般是动态对象。
		 * 覆盖其位置设置方法，如果位置移出了范围，且启动了四叉树检索，则重新构建节点
		 */ 
		override public function setPos(px:uint,py:uint):void
		{
			super.setPos(px,py);
			inuse = _controler.perception.Scene.Map.cameraView.containsPoint(pos);
			if(_qTree==null) return;
			_qTree = _qTree.reinsert(this,int(px),int(py));
		}
		
		/**
		 * 当非循环动作播放一次后，自动更换动作，由子类继承实现
		 */ 
		protected function autoChangeActon():void
		{
			
		}
		
		/**
		 * 脚印出现的时间间隔
		 */ 
		public function set FeetTime(u:Number):void
		{
			_feetTime = int(u*Global.FPS);
		}
		
		public function makeFeetStep():void
		{
			if(feetFile=='' || _feetTime==0) return;
			
			_feetSpend++;
			if(_feetSpend>_feetTime)
			{
				_feetSpend=0;
				_feetLOR=!_feetLOR;
				var feet:FeetStep = new FeetStep(controler.perception.Scene,feetFile,this,_feetLOR);
				(controler.perception.Scene as D5Scene).createEffect(feet);
			}
		}
		
		/**
		 * 运行Lua脚本
		 */ 
		public function runLua(src:String):void
		{
			myLua = new LuaAlchemy();
			myLua.setGlobal('me',this);
			var result:Array = myLua.doString(src);
		}
		/**
		 * 设置Lua脚本的URL并读取运行
		 */ 
		public function setLua(url:String):void
		{
			if(_lua_controler==null) _lua_controler = new BaseLuaControler(this);
			
			_lua_controler.scriptURL = url;
		}
		
		override public function renderMe():void
		{
			super.renderMe();
			if(action!=Actions.Stop && _currentFrame == graphics.getFrameTotal(Math.abs(directionNum))-1 && !_loop) autoChangeActon();
		}
		
		override public function clear():void
		{
			
			_qTree = null;
			super.clear();
		}
		
	}
}